import gtk.gdk
import os
import pkgutil
import imp

import handlepaths


plugins = {}

def load_plugins():
  "Load available plugins"
  print 'loading available plugins...'
  plugins_path = [handlepaths.get_path('plugins')]
  for loader, name, isPkg in pkgutil.iter_modules(plugins_path):
    file, pathname, description = imp.find_module(name, plugins_path)
    imp.load_module(name, file, pathname, description)

def register_plugin(name, plugin_class):
  "Register a new plugin"
  global plugins
  plugins[name] = plugin_class

def signal_proxy(signal, argc=0, args=None):
  "Call a signal by its name"
  for plugin in plugins.itervalues():
    if hasattr(plugin, signal):
      method = getattr(plugin, signal)
      if method and plugin.active:
        if argc == 0:
          method()
        elif argc == 1:
          method(args)
        elif argc == 2:
          method(args[0], args[1])
        elif argc == 3:
          method(args[0], args[1], args[2])
        elif argc == 4:
          method(args[0], args[1], args[2], args[3])
        elif argc == 5:
          method(args[0], args[1], args[2], args[3], args[4])

class GMediaCatalogPlugin(object):
  def __init__(self, name, version, description, author):
    "Module initialization"
    self.name = name
    self.version = version
    self.description = description
    self.author = author
    self.logger('init plugin v.%s' % version)
    self.active = True

  def load(self):
    "Plugin load"
    pass

  def unload(self):
    "Plugin unload"
    pass

  def reload(self):
    "Plugin reload"
    self.unload()
    self.load()

  def configure(self):
    "Plugin custom configuration"
    pass

  def update_ui(self):
    "Update UI for changes"
    pass

  def enable(self):
    "Enable the disabled plugin"
    self.active = True

  def disable(self):
    "Disable the plugin"
    self.active = False

  def logger(self, message):
    "Print a message from a plugin"
    print '[%s]: %s' % (self.name, message)

  def on_uiready(self, ui, connection):
    pass

  def on_closing(self):
    pass

  def on_closed(self):
    pass

  def on_terminate(self):
    pass
  
  def on_before_new_volume(self):
    pass

  def on_new_volume(self, name, description, fVolume):
    pass

  def on_after_new_volume(self, volume_id):
    pass

  def on_before_new_path(self):
    pass

  def on_new_path(self, volume_id, fFolder):
    pass

  def on_after_new_path(self, path_id):
    pass

  def on_before_new_file(self):
    pass

  def on_new_file(self, volume_id, folder_id, child, info, category_id):
    pass

  def on_after_new_file(self, file_id):
    pass

  def on_delete_volume(self, volume_id):
    pass
